package com.xiaomo.gitee.datamanager.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {

    private final int coreSize = 5;

    private final int maxSize = 10;

    //单位: s
    private final int keepAlive = 5;

    private final int arrayBlockQueueSize = 10;

    @Bean
    @Primary
    public Executor executor() {
        ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
        threadPoolExecutor.setCorePoolSize(coreSize);
        threadPoolExecutor.setMaxPoolSize(maxSize);
        threadPoolExecutor.setKeepAliveSeconds(keepAlive);
        threadPoolExecutor.setQueueCapacity(arrayBlockQueueSize);
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        threadPoolExecutor.initialize();
        return threadPoolExecutor;
    }

}
